home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Emulators / v2600 / Source.lha / Source / vmachine.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-25  |  13.7 KB  |  678 lines

  1. /*****************************************************************************
  2.  
  3.    This file is part of x2600, the Atari 2600 Emulator
  4.    ===================================================
  5.    
  6.    Copyright 1996 Alex Hornby. For contributions see the file CREDITS.
  7.  
  8.    This software is distributed under the terms of the GNU General Public
  9.    License. This is free software with ABSOLUTELY NO WARRANTY.
  10.    
  11.    See the file COPYING for details.
  12.    
  13.    $Id: vmachine.c,v 2.19 1996/11/24 16:55:40 ahornby Exp $
  14.  
  15.    Tweaked by Matthew Stroup for Amiga v2600, February 5, 1997.
  16.  
  17. ******************************************************************************/
  18.  
  19. /* 
  20.    The virtual machine. Contains the RIOT timer code, and hardware 
  21.    initialisation.
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include "types.h"
  27. #include "address.h"
  28. #include "options.h"
  29. #include "display.h"
  30. #include "raster.h"
  31. #include "cpu.h"
  32. #include "misc.h"
  33. #include "collision.h"
  34. #include "sound.h"
  35. #include "keyboard.h"
  36. #include "realjoy.h"
  37.  
  38. /* The Rom define might need altering for paged carts */
  39. /* Enough for 16k carts */
  40. int rom_size;
  41.  
  42. /* Used as a simple file buffer to store the data */
  43. UBYTE theCart[16384];
  44.  
  45. /* Pointer to start of ROM data */
  46. UBYTE *theRom;
  47.  
  48. /* Scratch area for those banking types that require memcpy() */
  49. UBYTE cartScratch[4096];
  50.  
  51. /* Area for those carts containing RAM */
  52. UBYTE cartRam[1024];
  53.  
  54. UBYTE theRam[128];
  55. UBYTE tiaRead[0x0e];
  56. UBYTE tiaWrite[0x2d];
  57. UBYTE keypad[2][4];
  58.  
  59. /* These don't strictly need so much space */
  60. UBYTE riotRead[0x298];
  61. UBYTE riotWrite[0x298];
  62.  
  63. /* 
  64.    Hardware addresses not programmer accessible 
  65.  */
  66.  
  67. /* Set if whole emulator is reset */
  68. int reset_flag = 0;
  69.  
  70. /* The timer resolution, can be 1,8,64,1024 */
  71. int timer_res = 32;
  72. int timer_count = 0;
  73. int timer_clks = 0;
  74. extern CLOCK clk;
  75. extern int beamadj;
  76.  
  77. /* Electron beam position */
  78. int ebeamx, ebeamy, sbeamx;
  79.  
  80. /* The state of the electron beam */
  81. #define VSYNCSTATE 1
  82. #define VBLANKSTATE 2
  83. #define HSYNCSTATE 4
  84. #define DRAWSTATE 8
  85. #define OVERSTATE 16
  86. int vbeam_state;        /* 1 2 8 or 16 */
  87. int hbeam_state;        /* 4 8 or 16 */
  88.  
  89. /* The tv size, varies with PAL/NTSC */
  90. int tv_width, tv_height, tv_vsync, tv_vblank, tv_overscan, tv_frame, tv_hertz,
  91.   tv_hsync;
  92.  
  93. /* The PlayField structure */
  94. struct PlayField
  95.   {
  96.     UBYTE pf0, pf1, pf2, ref;
  97.   }
  98. pf[2];
  99.  
  100. struct Paddle
  101.   {
  102.     int pos;
  103.     int val;
  104.   }
  105. paddle[4];
  106.  
  107. /* The player variables */
  108. struct Player
  109.   {
  110.     int x;
  111.     UBYTE grp;
  112.     UBYTE hmm;
  113.     UBYTE vdel;
  114.     UBYTE vdel_flag;
  115.     UBYTE nusize;
  116.     UBYTE reflect;
  117.     UBYTE mask;
  118.   }
  119. pl[2];
  120.  
  121. /* The element used in display lists */
  122. struct RasterChange
  123.   {
  124.     int x;            /* Position at which change happened */
  125.     int type;            /* Type of change */
  126.     int val;            /* Value of change */
  127.   };
  128.  
  129. #define MAXLIST 80
  130. /* The various display lists */
  131. struct RasterChange pl_change[2][MAXLIST], pf_change[1][MAXLIST], unified[MAXLIST];
  132.  
  133. /* The display list counters */
  134. int pl_change_count[2], pf_change_count[1], unified_count;
  135.  
  136. /* The missile an ball positions */
  137. struct Missile
  138.   {
  139.     int x;
  140.     UBYTE hmm;
  141.     UBYTE enabled;
  142.     UBYTE locked;
  143.     UBYTE width;
  144.     UBYTE vdel;
  145.     UBYTE vdel_flag;
  146.     UBYTE mask;
  147.   }
  148. ml[3];
  149.  
  150.  
  151.  
  152. /***************************************************************************
  153.                            Let the functions begin!
  154. ****************************************************************************/
  155.  
  156. /* Device independent screen initialisations */
  157. void
  158. init_screen (void)
  159. {
  160.   /* Set the electron beam to the top left */
  161.   ebeamx = -tv_hsync;
  162.   ebeamy = 0;
  163.   vbeam_state = VSYNCSTATE;
  164.   hbeam_state = OVERSTATE;
  165.  
  166.   tv_vsync = 3;
  167.   tv_hsync = 68;
  168.   switch (base_opts.tvtype) 
  169.     {
  170.     case NTSC:      
  171.       tv_width = 160;
  172.       tv_height = 192;
  173.       tv_vblank = 40;
  174.       tv_overscan = 30;
  175.       tv_frame = 262;
  176.       tv_hertz = 60;
  177.     break;
  178.     case PAL:
  179.     case SECAM:
  180.       tv_width = 160;
  181.       tv_height = 228;
  182.       tv_vblank = 48;
  183.       tv_overscan = 36;
  184.       tv_frame = 312;
  185.       tv_hertz = 50;
  186.       break;
  187.     }
  188. }
  189.  
  190. /* Initialise the RIOT (also known as PIA) */
  191. void
  192. init_riot (void)
  193. {
  194.   int i;
  195.   /* Wipe the RAM */
  196.   for (i = 0; i < 0x80; i++)
  197.     theRam[i] = 0;
  198.   /* Set the timer to zero */
  199.   riotRead[INTIM] = 0;
  200.   /* Set the joysticks and switches to input */
  201.   riotWrite[SWACNT] = 0;
  202.   riotWrite[SWBCNT] = 0;
  203.  
  204.   /* Centre the joysticks */
  205.   riotRead[SWCHA] = 0xff;
  206.   riotRead[SWCHB] = 0x0b;
  207.  
  208.   /* Set the counter resolution */
  209.   timer_res = 32;
  210.   timer_count = 0;
  211.   timer_clks = 0;
  212. }
  213.  
  214. /* Initialise the television interface adaptor (TIA) */
  215. void
  216. init_tia (void)
  217. {
  218.   int i;
  219.   tiaWrite[CTRLPF] = 0x00;
  220.   for (i = 0; i < 2; i++)
  221.     {
  222.       pl[i].hmm = 0x0;
  223.       pl[i].x = 0x0;
  224.       pl[i].nusize = 0;
  225.       pl[i].grp = 0;
  226.       pl[i].vdel = 0;
  227.       pl[i].vdel_flag = 0;
  228.       pl_change_count[i] = 0;
  229.     }
  230.  
  231.   pl[0].mask = PL0_MASK;
  232.   pl[1].mask = PL1_MASK;
  233.   ml[0].mask = ML0_MASK;
  234.   ml[1].mask = ML1_MASK;
  235.   reset_collisions ();
  236.  
  237.   pf_change_count[0] = 0;
  238.   unified_count = 0;
  239.   for (i = 0; i < 3; i++)
  240.     {
  241.       ml[i].enabled = 0;
  242.     }
  243.   
  244.   tiaWrite[VBLANK] = 0;
  245.   tiaRead[INPT4] = 0x80;
  246.   tiaRead[INPT5] = 0x80;
  247.  
  248.   /* Set up the colour table */
  249.   colour_table[P0M0_COLOUR]=0xf4;
  250.   colour_table[P1M1_COLOUR]=0xf4;
  251.   colour_table[PFBL_COLOUR] = 0xf6;
  252.   colour_table[BK_COLOUR] = 0xf6;
  253. }
  254.  
  255. void
  256. init_memory(void)
  257. {
  258.   int i;
  259.   for(i=0;i<1024; i++)
  260.     cartRam[i]=0;
  261. }
  262.  
  263. void
  264. init_banking (void)
  265. {
  266.   /* Set to the first bank */
  267.   if (Verbose) printf ("rom_size is %d\n", rom_size);
  268.   if (rom_size == 2048)
  269.     theRom = &theCart[rom_size - 2048];
  270.   else
  271.     theRom = &theCart[rom_size - 4096];
  272.   
  273.   switch(base_opts.bank)
  274.     {
  275.     case 3:
  276.       /* Parker Brothers 8k E0 */
  277.       memcpy(&cartScratch[0xc00],&theCart[0x1c00],1024);
  278.       memcpy(&cartScratch[0],&theCart[0],3072);
  279.       theRom=cartScratch;
  280.       break;
  281.     default:
  282.       break;
  283.     }
  284. }
  285.  
  286. /* Main hardware startup */
  287. void
  288. init_hardware (void)
  289. {
  290.   init_screen ();
  291.   init_riot ();
  292.   init_tia ();
  293.   init_raster ();
  294.   init_memory();
  295.   init_banking();
  296.   init_cpu (0xfffc);
  297. }
  298.  
  299. /* Do a raster change */
  300. __inline void do_raster_change (int i, int type, int val, struct RasterChange *rc)
  301. {
  302.   if (beamadj == 0)
  303.     {
  304.       printf ("BEAMADJ == 0\n");
  305.       show ();
  306.     }
  307.   rc->x = ebeamx + beamadj;
  308.   rc->type = type;
  309.   rc->val = val;
  310. }
  311.  
  312. /* Do a raster change on the unified list */
  313. /* type: type of change */
  314. /* val: value of change */
  315. __inline void do_unified_change (int type, int val)
  316. {
  317.   if (unified_count < MAXLIST)
  318.     {
  319.       unified[unified_count].x = ebeamx + beamadj;
  320.       unified[unified_count].type = type;
  321.       unified[unified_count].val = val;
  322.       unified_count++;
  323.     }
  324. }
  325.  
  326. /* Do a player raster change */
  327. /* i: player to change. 0 or 1 */
  328. /* type: type of change */
  329. /* val: value of change */
  330. __inline void do_plraster_change (int i, int type, int val)
  331. {
  332.   int plc = pl_change_count[i];
  333.   /*printf("Raster change i=%d, x=%d, type=%d, val=%d\n", i, x, type, val); */
  334.   if (plc < MAXLIST)
  335.     {
  336.       do_raster_change (i, type, val, &pl_change[i][plc]);
  337.       if (type == 1)
  338.     pl_change[i][plc].x -= 3;
  339.       pl_change_count[i]++;
  340.     }
  341. }
  342.  
  343. /* Do a playfield raster change */
  344. /* i: playfield to change. Depreciated, as 0 is now only one used */
  345. /* type: type of change */
  346. /* val: value of change */
  347. __inline void do_pfraster_change (int i, int type, int val)
  348. {
  349.   int pfc = pf_change_count[i];
  350.   /*  
  351.      if(ebeamy>=100) {
  352.      printf("Raster change i=%d, x=%d, type=%d, val=%d\n", 
  353.      i, ebeamx+beamadj, type, val);
  354.      show();
  355.      }
  356.    */
  357.   if (pfc < MAXLIST)
  358.     {
  359.       do_raster_change (i, type, val, &pf_change[i][pfc]);
  360.       pf_change_count[i]++;
  361.     }
  362. }
  363.  
  364.  
  365. /* Use a unified change */
  366. /* rc: unified change structure to use */
  367. __inline void use_unified_change (struct RasterChange *rc)
  368. {
  369.   switch (rc->type)
  370.     {
  371.     case 0:
  372.       /* P0MO colour */
  373.       colour_table[P0M0_COLOUR] = rc->val;
  374.       break;
  375.     case 1:
  376.       /* POM0 colour */
  377.       colour_table[P1M1_COLOUR] = rc->val;
  378.       break;
  379.     case 2:
  380.       /* PFBL colour */
  381.       colour_table[PFBL_COLOUR] = rc->val;
  382.       break;
  383.     case 3:
  384.       /* BK colour */
  385.       colour_table[BK_COLOUR] = rc->val;
  386.       break;
  387.     case 4:
  388.       /* Priority change Normal */
  389.       if(rc->val)
  390.     norm_val=1;
  391.       else
  392.     norm_val=0;
  393.       colour_lookup=colour_ptrs[norm_val][scores_val];
  394.       break;
  395.     case 5:
  396.       /* Priority change Scores */
  397.       if(rc->val)
  398.     {
  399.       if(rc->x < 80)
  400.         scores_val=1;
  401.       else
  402.         scores_val=2;
  403.     }
  404.       else
  405.     scores_val=0;
  406.       colour_lookup=colour_ptrs[norm_val][scores_val];
  407.       break;
  408.     }
  409. }
  410.  
  411. /* Use a playfield change */
  412. /* pl: playfield to change */
  413. /* rc: change to make */
  414. __inline void use_pfraster_change (struct PlayField *pl, struct RasterChange *rc)
  415. {
  416.   switch (rc->type)
  417.     {
  418.     case 0:
  419.       /* PF0 */
  420.       pl->pf0 = rc->val;
  421.       break;
  422.     case 1:
  423.       /* PF1 */
  424.       pl->pf1 = rc->val;
  425.       break;
  426.     case 2:
  427.       /* PF2 */
  428.       pl->pf2 = rc->val;
  429.       break;
  430.     case 3:
  431.       /* Reflection */
  432.       pl->ref = rc->val;
  433.       break;
  434.     }
  435. }
  436.  
  437. /* Use a player change */
  438. /* pl: player to change */
  439. /* rc: change to make */
  440. __inline void use_plraster_change (struct Player *pl, struct RasterChange *rc)
  441. {
  442.   switch (rc->type)
  443.     {
  444.     case 0:
  445.       /* GRP */
  446.       pl->grp = rc->val;
  447.       break;
  448.       /* Vertical delay */
  449.     case 1:
  450.       pl->vdel = pl->grp;
  451.       break;
  452.     }
  453. }
  454.  
  455.  
  456. int
  457. do_paddle (int padnum)
  458. {
  459.   int res = 0x80;
  460.   int x;
  461.   if ((tiaWrite[VBLANK] & 0x80) == 0)
  462.     {
  463.       x = 320 - mouse_position ();
  464.       x = x * 45;
  465.       x += paddle[padnum].val;
  466.       if (x > clk)
  467.     res = 0x00;
  468.     }
  469.   return res;
  470. }
  471.  
  472. /* Calculate the keypad rows */
  473. /* i.e. when reading from INPTx we don't know the row */
  474. UBYTE
  475. do_keypad (int pad, int col)
  476. {
  477.   UBYTE res= 0x80;
  478.  
  479.   read_keypad(pad);
  480.   
  481.   /* Bottom row */
  482.   if(pad==0) {
  483.   if( (riotWrite[SWCHA] & 0x80) && keypad[pad][col]==3)
  484.     res=0x00;
  485.   /* Third row */
  486.   if( (riotWrite[SWCHA] & 0x40) && keypad[pad][col]==2)
  487.     res=0x00;
  488.   if( (riotWrite[SWCHA] & 0x20) && keypad[pad][col]==1)
  489.     res=0x00;
  490.   if( (riotWrite[SWCHA] & 0x10) && keypad[pad][col]==0)
  491.     res=0x00;
  492.   } 
  493.   else {
  494.   /* Bottom row */
  495.   if( (riotWrite[SWCHA] & 0x80) && keypad[pad][col]==3)
  496.     res=0x00;
  497.   /* Third row */
  498.   if( (riotWrite[SWCHA] & 0x40) && keypad[pad][col]==2)
  499.     res=0x00;
  500.   if( (riotWrite[SWCHA] & 0x20) && keypad[pad][col]==1)
  501.     res=0x00;
  502.   if( (riotWrite[SWCHA] & 0x10) && keypad[pad][col]==0)
  503.     res=0x00;
  504.   }
  505.   return res;
  506. }
  507.  
  508.  
  509. /* 
  510.    Called when the timer is set .
  511.    Note that res is the bit shift, not absolute value.
  512.    Assumes that any timer interval set will last longer than the instruction
  513.    setting it.
  514.  */
  515. /* res: timer interval resolution as a bit shift value */
  516. /* count: the number of intervals to set */
  517. /* clkadj: the number of CPU cycles into the current instruction  */
  518. void
  519. set_timer (int res, int count, int clkadj)
  520. {
  521.   timer_count = count << res;
  522.   timer_clks = clk + clkadj;
  523.   timer_res = res;
  524. }
  525.  
  526. /* New timer code, now only called on a read of INTIM */
  527. /* clkadj: the number of CPU cycles into the current instruction  */
  528. /* returns: the current timer value */
  529. UBYTE
  530. do_timer (int clkadj)
  531. {
  532.   UBYTE result;
  533.   int delta;
  534.   int value;
  535.  
  536.   delta = clk - timer_clks;
  537.   value = delta >> timer_res;
  538.   if (delta <= timer_count)
  539.     {                /* Timer is still going down in res intervals */
  540.       result = value;
  541.     }
  542.   else
  543.     {
  544.       if (value == 0)
  545.     /* Timer is in holding period */
  546.     result = 0;
  547.       else
  548.     {
  549.       /* Timer is descending from 0xff in clock intervals */
  550.       set_timer (0, 0xff, clkadj);
  551.       result = 0;
  552.     }
  553.     }
  554.  
  555.   /*  printf("Timer result=%d\n", result); */
  556.   return result;
  557. }
  558.  
  559. /* Do the screen related part of a write to VBLANK */
  560. /* b: the byte written */
  561. void
  562. do_vblank (UBYTE b)
  563. {
  564.   if (b & 0x02)
  565.     {
  566.       /* Start vertical blank */
  567.       vbeam_state = VBLANKSTATE;
  568.       /* Also means we can update screen */
  569.       sound_update ();
  570.       update_realjoy ();
  571.       tv_event ();
  572.       tv_display ();
  573. //    /* Only wait if we're on a faster display */
  574. //    if (base_opts.rr == 1)
  575. //     limiter_sync ();
  576.     }
  577.   else
  578.     {
  579.       /* End vblank, and start first hsync drawing */
  580.       int i;
  581.  
  582.       vbeam_state = DRAWSTATE;
  583.       hbeam_state = HSYNCSTATE;
  584.       /* Set up the screen */
  585.       for (i = 0; i < unified_count; i++)
  586.     use_unified_change (&unified[i]);
  587.       /* Hope for a WSYNC, but just in case */
  588.       ebeamx = -tv_hsync;
  589.       ebeamy = 0;
  590.     }
  591. }
  592.  
  593.  
  594. /* do a horizontal sync */
  595. void
  596. do_hsync (void)
  597. {
  598.   /* Only perform heavy stuff if electron beam is in correct position */
  599.   if (vbeam_state == DRAWSTATE && (ebeamx > -tv_hsync))
  600.     {
  601.       tv_raster (ebeamy);
  602.       /* Fix the clock value */
  603.       clk += (ebeamx - tv_width) / 3;
  604.       ebeamy++;
  605.     }
  606.   hbeam_state = HSYNCSTATE;
  607.   ebeamx = -tv_hsync;
  608.   sbeamx = 0;
  609. }
  610.  
  611. /* Main screen logic */
  612. /* clks: CPU clock length of last instruction */
  613. void
  614. do_screen (int clks)
  615. {
  616.   switch (vbeam_state)
  617.     {
  618.     case VSYNCSTATE:
  619.     case VBLANKSTATE:
  620.       switch (hbeam_state)
  621.     {
  622.     case HSYNCSTATE:
  623.       ebeamx += clks * 3;
  624.       if (ebeamx >= 0)
  625.         {
  626.           hbeam_state = DRAWSTATE;
  627.         }
  628.       break;
  629.     case DRAWSTATE:
  630.       ebeamx += clks * 3;
  631.       if (ebeamx >= tv_width)
  632.         {
  633.           ebeamx -= (tv_hsync + tv_width);
  634.           /* Insert hsync stuff here */
  635.           sbeamx = ebeamx;
  636.           hbeam_state = HSYNCSTATE;
  637.         }
  638.       break;
  639.     case OVERSTATE:
  640.       break;
  641.     }
  642.       break;
  643.     case DRAWSTATE:
  644.       switch (hbeam_state)
  645.     {
  646.     case HSYNCSTATE:
  647.       ebeamx += clks * 3;
  648.       if (ebeamx >= 0)
  649.         {
  650.           hbeam_state = DRAWSTATE;
  651.         }
  652.       break;
  653.     case DRAWSTATE:
  654.       ebeamx += clks * 3;
  655.       if (ebeamx >= tv_width)
  656.         {
  657.           /* Insert hsync stuff here */
  658.           sbeamx = ebeamx;
  659.           ebeamx -= (tv_hsync + tv_width);
  660.           tv_raster (ebeamy);
  661.           ebeamy++;
  662.           hbeam_state = HSYNCSTATE;
  663.         }
  664.       if (ebeamy >= tv_height + tv_overscan)
  665.         {
  666.           vbeam_state = OVERSTATE;
  667.           ebeamy = 0;
  668.         }
  669.       break;
  670.     case OVERSTATE:
  671.       break;
  672.     }
  673.       break;
  674.     case OVERSTATE:
  675.       break;
  676.     }
  677. }
  678.